package com.example.factory.api.support;

import com.example.factory.core.domain.FactoryEntity;
import com.example.factory.core.repository.FactoryRepository;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * @author Lei Tao
 */
@Component
public class FactorySupport implements InitializingBean {
    private final FactoryRepository factoryRepository;

    public FactorySupport(FactoryRepository factoryRepository) {
        this.factoryRepository = factoryRepository;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        long count = factoryRepository.selectAllCount();
        if (count < 1) {
            FactoryEntity factoryEntity = new FactoryEntity();
            factoryEntity.setName("初始化工厂");
            factoryEntity.setType("初始化了类型");
            factoryEntity.setBoss("初始化Boss");
            factoryEntity.setPhone("00000");
            factoryEntity.setPosition("初始化地理位置");
            factoryEntity.setScale("初始化工厂规模");
            factoryRepository.save(factoryEntity);
        } else if (count > 1) {
            throw new Exception("工厂存在错误信息");
        }
    }
}
